#!/bin/sh
# javaiffind - Find the most current version of JAVA JDK or JRE.
# Invoked concurrently by /etc/profile.d/java and /etc/init.d/javaif.sh.
# Returns path to JDK/JRE and exit code 0 if any found.

. /etc/javaif.conf

JAVADIRS="$(ls -d $(echo $JAVAHOMES | tr '\n' ' ') 2>/dev/null)"
NEWESTJAVA=''
if [ "$JAVADIRS" ]; then
 NEWESTVERSION=''
 for ONEJAVADIR in $JAVADIRS; do
  if [ -x $ONEJAVADIR/bin/java ]; then
   JAVAVER="$($ONEJAVADIR/bin/java -version 2>&1 | grep 'version' | grep -o '".*"' | tr -d \" | tr _ .)"
   if [ -z $NEWESTVERSION ] || vercmp $JAVAVER gt $NEWESTVERSION; then
    NEWESTVERSION=$JAVAVER
    NEWESTJAVA=$ONEJAVADIR
   elif vercmp $JAVAVER eq $NEWESTVERSION \
     && ! echo $NEWESTJAVA | grep -q '/jdk' \
     && echo $ONEJAVADIR | grep -q '/jdk' ; then
    NEWESTVERSION=$JAVAVER
    NEWESTJAVA=$ONEJAVADIR
   fi
  fi
 done
fi
echo "$NEWESTJAVA"
[ "$NEWESTJAVA" ]
exit
